﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data;
using ZhiAo.WebLib.Data;

namespace ZhiAo.Management
{
    public class TabDataQuery
    {
        public string trHtml = "";
        public string thHtml = "";

        //绑定数据
        public string BindData(string tableName)
        {
            DataTable dtData = GetDtData(tableName);
            string regHtml = BindData(dtData);
            return regHtml;
        }

        public string BindData(DataTable dtData)
        {
            string regHtml = "";
            string trClass = "color1";
            string repHtml = trHtml;
            ArrayList arrGvlist = new ArrayList();
            foreach (DataRow dr in dtData.Rows)
            {
                repHtml = trHtml;
                arrGvlist = GetBindField();
                if (trClass == "color0")
                    trClass = "color1";
                else
                    trClass = "color0";
                foreach (string field in arrGvlist)
                {
                    repHtml = repHtml.Replace("{#" + field + "#}", dr[field].ToString().Trim());
                    repHtml = repHtml.Replace("{$color$}", trClass);
                }
                regHtml += repHtml;
            }
            return regHtml;
        }



        //获取数据
        public DataTable GetDtData(string tableName)
        {
            string sql = "SELECT ROW_NUMBER() OVER (ORDER BY SHI,XIAN) AS ROWID {0} FROM " + tableName + "";
            ArrayList arrGvlist = GetBindField();
            string fieldNames = "";
            foreach (string field in arrGvlist)
            {
                fieldNames += "," + field;
            }
            sql = string.Format(sql, fieldNames);
            DataTable retDt = DBHelper.GetDataTable(sql);
            return retDt;

        }

        //获取列表字段
        public ArrayList GetBindField()
        {
            ArrayList arrGvlist = new ArrayList();
            string fieldName = "";
            Regex reg = new Regex("{#([^#}].*)#}");
            MatchCollection Matchs = reg.Matches(trHtml);
            foreach (Match item in Matchs)
            {
                fieldName = item.Groups[1].Value;
                if (!arrGvlist.Contains(fieldName))
                    arrGvlist.Add(fieldName);
            }
            return arrGvlist;
        }

        //获取表头备注
        public List<string> GetHeadMark()
        {
            string columName = "";
            List<string> lstHead = new List<string>();
            Regex reg = new Regex(@"<th[^>]*?>([^</]*?)</th>");
            MatchCollection Matchs = reg.Matches(thHtml);
            foreach (Match item in Matchs)
            {
                columName = item.Groups[1].Value;
                lstHead.Add(columName);
            }
            return lstHead;

        }
    }
}
